# -*- coding: utf-8 -*-
from datetime import timedelta
from time import time
from utils.operators.rich_sql_sensor import RichSqlSensor
# from jms.dm.dm_sqs_false_sign_detail_dt import jms_dm__dm_sqs_false_sign_detail_dt

# doris表名
doris_table = "dm_sqs_false_sign_detail_dt"
# label名
label = f"{doris_table}_{int(time())}"
# hive中表名
hive_table = "dm_sqs_false_sign_detail_dt"
# hdfs文件路径
hdfs_path = "hdfs://{{var.value.hadoop_namespace}}/dw/hive/jms_dm.db/external/"
timeout = timedelta(hours=2).seconds
# 判断当天分区是否存在
file_path = f'"{hdfs_path}{hive_table}/dt={{{{ execution_date | cst_ds }}}}/*"'
partition_lists = []
path_lists = []
for i in range(0, 2):
    if i == 0:
        partition_lists.append("p{{ execution_date | cst_ds_nodash }}")
        path_lists.append(file_path)
    else:
        partition_lists.append("p{{ execution_date | date_add(-" + str(i) + ") | cst_ds_nodash }}")
        path_lists.append(f'"{hdfs_path}{hive_table}/dt={{{{ execution_date | date_add(-{i}) | cst_ds }}}}/*"')

partition_str = ",".join(partition_lists)
path_str = ",".join(path_lists[::-1])
doris_jms_dm__dm_sqs_false_sign_detail_dt = RichSqlSensor(
    task_id='doris_jms_dm__dm_sqs_false_sign_detail_dt',
    pool='broker_load_pool',
    email=['jokic.wang@jtexpress.com'],
    task_concurrency=1,
    conn_id='new_doris',
    pre_sql=f"""
                 TRUNCATE TABLE jms_dm.{doris_table} PARTITION ({partition_str});
                 LOAD LABEL jms_dm.{label} (
                     DATA INFILE({path_str})
                     INTO TABLE {doris_table}
                     FORMAT AS 'PARQUET' (
                                            statistic_date ,
                                            waybill_no ,
                                            work_order_no ,
                                            agent_code ,
                                            delivery_code ,
                                            network_code ,
                                            network_name ,
                                            network_type ,
                                            agent_name ,
                                            virt_code ,
                                            virt_name ,
                                            customer_code ,
                                            customer_name ,
                                            order_create_time ,
                                            area_info_id ,
                                            area_info_code ,
                                            area_info_desc ,
                                            provider_id ,
                                            provider_desc ,
                                            city_id ,
                                            city_desc ,
                                            area_id ,
                                            area_desc ,
                                            is_enable ,
                                            is_suspend ,
                                            delivery_name ,
                                            pdd_create_time ,
                                            work_order_type ,
                                            problem_type_code ,
                                            problem_type_name ,
                                            problem_description ,
                                            customer_type ,
                                            send_type ,
                                            sign_type ,
                                            is_reback ,
                                            is_phone_contact ,
                                            is_promise ,
                                            instore_scantime ,
                                            outstore_scantime ,
                                            false_type
                                            )
                                            columns from path as (dt)
                                            set (
                                            statistic_date = statistic_date ,
                                            waybill_no = waybill_no ,
                                            work_order_no = work_order_no ,
                                            agent_code = agent_code ,
                                            delivery_code = delivery_code ,
                                            network_code = network_code ,
                                            network_name = network_name ,
                                            network_type = network_type ,
                                            agent_name = agent_name ,
                                            virt_code = virt_code ,
                                            virt_name = virt_name ,
                                            customer_code = customer_code ,
                                            customer_name = customer_name ,
                                            order_create_time = order_create_time ,
                                            area_info_id = area_info_id ,
                                            area_info_code = area_info_code ,
                                            area_info_desc = area_info_desc ,
                                            provider_id = provider_id ,
                                            provider_desc = provider_desc ,
                                            city_id = city_id ,
                                            city_desc = city_desc ,
                                            area_id = area_id ,
                                            area_desc = area_desc ,
                                            is_enable = is_enable ,
                                            is_suspend = is_suspend ,
                                            delivery_name = delivery_name ,
                                            pdd_create_time = pdd_create_time ,
                                            work_order_type = work_order_type ,
                                            problem_type_code = problem_type_code ,
                                            problem_type_name = problem_type_name ,
                                            problem_description = problem_description ,
                                            customer_type = customer_type ,
                                            send_type = send_type ,
                                            sign_type = sign_type ,
                                            is_reback = is_reback ,
                                            is_phone_contact = is_phone_contact ,
                                            is_promise = is_promise ,
                                            instore_scantime = instore_scantime ,
                                            outstore_scantime = outstore_scantime ,
                                            false_type = false_type
                                            )
                 )
                 WITH BROKER '{{{{ var.json.doris_brokers | random_choice }}}}'
                 PROPERTIES ('timeout'='{timeout}', 'max_filter_ratio'='0.0')""",
    poke_sql=f"SHOW LOAD FROM jms_dm WHERE label = '{label}' ORDER BY CreateTime DESC LIMIT 1",
    sql_on_kill=f"CANCEL LOAD FROM jms_dm WHERE LABEL = '{label}'",
    success=lambda r: r[2] == 'FINISHED',
    failure=lambda r: (r[2] is not None and r[2] == 'CANCELLED', str(r[7])),
    poke_interval=60,
    execution_timeout=timedelta(seconds=timeout + 120), )

# doris_jms_dm__dm_sqs_false_sign_detail_dt << [jms_dm__dm_sqs_false_sign_detail_dt]


